package view

import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import ext.toRad
import kotlin.math.cos
import kotlin.math.sin

class ProgressBarView : View {
    companion object {
        private const val THINKNESS_P = 0.5f
        private const val MARGIN_COLOR = 0xffffbb33.toInt()
        private const val TEXT_COLOR = Color.BLACK
    }

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    private var with = 0f
    private var height = 0f
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val rectF = RectF()
    var animator: ObjectAnimator? = null

    var steps = emptyArray<Step>()
        set(value) {
            field = value
            var s = 0f
            for (i in value) {
                s += i.time
            }
            maxValue = s
            currentValue = maxValue
            invalidate()
        }

    var currentValue = 0f
        set(value) {
            if (value != field) {
                field = value
                invalidate()
            }
        }

    var maxValue = 0f


    fun start(value: Float = 0f) {
        cancel()
        animator = ObjectAnimator.ofFloat(this, "currentValue", value, maxValue)
        animator!!.interpolator = LinearInterpolator()
        animator!!.duration = (maxValue - value).toLong() * 1000
        animator!!.start()
    }

    fun cancel() {
        if (animator != null) {
            animator!!.cancel()
            animator = null
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        with = w.toFloat()
        height = h.toFloat()
    }

    override fun onDraw(canvas: Canvas) {
        val x0 = with / 2
        val y0 = height / 2
        val a = if (width < height) with else height
        val r = a * 0.5f / (1 + 0.5f * THINKNESS_P)
        val d = r * THINKNESS_P

        paint.color = Color.GRAY
        paint.strokeWidth = d
        paint.style = Paint.Style.STROKE
        paint.strokeCap = Paint.Cap.ROUND
        canvas.drawCircle(x0, y0, r, paint)

        val max = maxValue

        paint.color = MARGIN_COLOR
        val angle = -(currentValue / max * 360)
        rectF.left = x0 - r
        rectF.top = y0 - r
        rectF.right = x0 + r
        rectF.bottom = y0 + r
        canvas.drawArc(rectF, -90f, angle, false, paint)

        textPaint.color = TEXT_COLOR
        textPaint.textSize = r * 0.5f
        textPaint.textAlign = Paint.Align.CENTER

        val measure = textPaint.fontMetrics
        val offset = (measure.top + measure.bottom) / 2

        val s = "${max.toInt() - currentValue.toInt()}s"
        canvas.drawText(s, x0, y0 - offset, textPaint)
        //drawStep(canvas, x0, y0, r)
    }

    private fun drawStep(canvas: Canvas, x0: Float, y0: Float, r: Float) {
        textPaint.textSize = 20f
        var time = 0f
        val max = maxValue
        val offset = (textPaint.fontMetrics.top + textPaint.fontMetrics.bottom) / 2
        for (step in steps) {
            val angle = (time / max * 360f + 90).toRad()
            val x = x0 + r * cos(angle)
            val y = y0 - r * sin(angle) - offset
            canvas.drawText(step.name, x, y, textPaint)
            time += step.time
        }
    }

    class Step(val name: String, val time: Float)

}

